﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GcmpModelInterface.h"
#include "GbmpQuickCast.h"
#include "UnitName.h"

namespace gcmp
{
    enum class BaseUnit;
    class IUnitDimension;

    struct GCMP_MODEL_INTERFACE_EXPORT UnitDimensionNames
    {
        UnitDimensionNames()
        {
            LengthUnit = LengthUnitName::UndefinedUnit;  
            AngleUnit = AngleUnitName::UndefinedUnit;
            MassUnit = MassUnitName::UndefinedUnit;
            TimeUnit = TimeUnitName::UndefinedUnit;
            CurrentUnit = CurrentUnitName::UndefinedUnit;
            TemperatureUnit = TemperatureUnitName::UndefinedUnit;
            LuminousIntensityUnit = LuminousIntensityUnitName::UndefinedUnit;
        }

        void SetUnits(
            LengthUnitName lengthUnit,
            AngleUnitName angleUnit,
            MassUnitName massUnit,
            TimeUnitName timeUnit,
            CurrentUnitName currentUnit,
            TemperatureUnitName temperatureUnit = TemperatureUnitName::UndefinedUnit,
            LuminousIntensityUnitName luminousIntensityUnit = LuminousIntensityUnitName::UndefinedUnit)
        {
            LengthUnit = lengthUnit;
            AngleUnit = angleUnit;
            MassUnit = massUnit;
            TimeUnit = timeUnit;
            CurrentUnit = currentUnit;
            TemperatureUnit = temperatureUnit;
            LuminousIntensityUnit = luminousIntensityUnit;
        }

        LengthUnitName                 LengthUnit;                 //长度单位
        AngleUnitName                  AngleUnit;                  //角度单位
        MassUnitName                   MassUnit;                   //质量单位
        TimeUnitName                   TimeUnit;                   //时间单位
        CurrentUnitName                CurrentUnit;                //电流单位
        TemperatureUnitName            TemperatureUnit;            //温度单位
        LuminousIntensityUnitName      LuminousIntensityUnit;      //亮度单位
    };

    /// \brief 单位转换接口工具类
    class GCMP_MODEL_INTERFACE_EXPORT UnitConverter
    {
    public:
        /// \brief 转换单位
        ///
        /// \param pUnitDimension 单位量纲
        /// \param parameterValue 要转换的参数值
        /// \param originUnits 转换之前的单位名集合
        /// \param convertedUnits 转换后的单位名集合
        /// \param convertedUnitRatio 转换后的单位的系数, 默认传入1.0
        /// \return double 单位转换后，会得到一个转换后的系数，该系数乘以传入的parameterValue，得到返回值
        ///convertedUnitRatio一般传入值为1.0，表示原单位和转换后的系数保持1:1的关系，只在处理单位类型不能转换的情况下，才需要额外设置。
        ///例如：焦每千克（J/kg）1m2/s2转换为千焦每千克（KJ/Kg）1000m2/s2， 转换后的单位中有1000这个系数，这个系数并不能从单位类型中转换得到，
        ///此情况下convertedUnitRatio应该设置为1000.0, 反之如果从千焦每千克（KJ/Kg）1000m2/s2转换为焦每千克（J/kg）1m2/s2，则convertedUnitRatio应该设置为0.001.
        ///注意：调用该接口之前需要先调用SetExponent接口设置7个基本单位对应的指数，否则转换会得到错误的结果
        static double Convert(
            const IUnitDimension* pUnitDimension,
            double parameterValue,
            const UnitDimensionNames& originUnits,
            const UnitDimensionNames& convertedUnits,
            double convertedUnitRatio = 1.0);
    };
}

